# Copyright 2021 Josh Newans
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import rclpy
from rclpy.node import Node

from std_msgs.msg import Header
from geometry_msgs.msg import PoseWithCovariance #odom的pose的格式
from geometry_msgs.msg import PoseStamped #ap/pose的格式
from geometry_msgs.msg import Pose #ap/pose的格式
from geometry_msgs.msg import TwistWithCovariance #odom的twist的格式 
from geometry_msgs.msg import TwistStamped #ap/twist的格式
from geometry_msgs.msg import Twist #ap/pose的格式
from nav_msgs.msg import Odometry #ROS的odom的格式


covariance = [
    0.1, 0.0, 0.0, 0.0, 0.0, 0.0,  # x 坐标的方差及其与其他变量的协方差
    0.0, 0.1, 0.0, 0.0, 0.0, 0.0,  # y 坐标的方差及其与其他变量的协方差
    0.0, 0.0, 0.1, 0.0, 0.0, 0.0,  # z 坐标的方差及其与其他变量的协方差
    0.0, 0.0, 0.0, 0.1, 0.0, 0.0,  # 滚转角的方差
    0.0, 0.0, 0.0, 0.0, 0.1, 0.0,  # 俯仰角的方差
    0.0, 0.0, 0.0, 0.0, 0.0, 0.1   # 偏航角的方差
]

class TwistUnstamper(Node):

    def __init__(self):
        super().__init__('pose_twist')#节点名称

        self.declare_parameter("child_frame_id", "")
        self.frame_id = str(self.get_parameter("child_frame_id").value)

        #self.publisher_ = self.create_publisher(PoseWithCovariance, 'pose_out', 10) #
        #self.publisher_ = self.create_publisher(TwistWithCovariance, 'twist_out', 10)
        self.publisher_ = self.create_publisher(Odometry, 'odom', 10)

        self.posewithcovariance = PoseWithCovariance()
        self.twistwithcovariance = TwistWithCovariance()#由Pose改为这个了

        self.subscription = self.create_subscription(
            PoseStamped,
            'ap/pose/filtered',
            self.listener_callback_pose,
            10)
        self.subscription = self.create_subscription(
            TwistStamped,
            'ap/twist/filtered',
            self.listener_callback_twist,
            10)
        
        self.subscription  # prevent unused variable warning

    def listener_callback_pose(self, pose):

        # posewithcovariance = PoseWithCovariance()
        self.posewithcovariance = pose.pose
        # self.posewithcovariance.position = pose
        # self.posewithcovariance.covariance = covariance
        self.publish_odom()

    def listener_callback_twist(self, twist):

        # twistwithcovariance = TwistWithCovariance()
        self.twistwithcovariance = twist.twist
        # self.twistwithcovariance.linear = twist
        # self.twistwithcovariance.covariance = covariance
        self.publish_odom()

    def publish_odom(self):

        odom_msg = Odometry()

        odom_msg.child_frame_id = "child_id"
        odom_msg.header.frame_id = "base_link"
        odom_msg.header.stamp = self.get_clock().now().to_msg()
        # odom_msg.pose = self.posewithcovariance.orientation + self.posewithcovariance.position
        # odom_msg.twist = self.twistwithcovariance.angular +self.twistwithcovariance.linear
        odom_msg.pose = self.posewithcovariance
        odom_msg.twist = self.twistwithcovariance
    

        self.publisher_.publish(odom_msg)
        # self.get_logger().info('X: "%f"' % outMsg.twist.linear.x)


def main(args=None):
    rclpy.init(args=args)

    twist_unstamper = TwistUnstamper()
    rclpy.spin(twist_unstamper)

    twist_unstamper.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()
